<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" :title="getTitle" width="500px">
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <a-button type="primary" @click="handleCreate"> 新增 </a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'clarity:note-edit-line',
                tooltip: '编辑',
                onClick: handleEdit.bind(null, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                tooltip: '删除',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
  </BasicDrawer>
  <DictItemModal @register="registerModal" @success="handleSuccess" :dictId="dictId" />
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref } from 'vue';
  import { dictItemColumns, dictItemSearchFormSchema } from '../dict.data';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { itemList, deleteItem } from '/@/api/system/dict/dict.api';
  import DictItemModal from './DictItemModal.vue';
  import { useModal } from '/@/components/Modal';

  export default defineComponent({
    name: 'DictItemDrawer',
    components: { BasicDrawer, BasicTable, TableAction, DictItemModal },
    emits: ['success', 'register'],

    setup(_, { emit }) {
      const dictId = ref('');
      const [registerModal, { openModal: openModal }] = useModal();
      const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
        dictId.value = data.id;
        console.log('dictId.value', dictId.value);
        setProps({ searchInfo: { dictId: unref(dictId) } });
        setDrawerProps({ confirmLoading: false });
        // 需要在setFieldsValue之前先填充treeData，否则Tree组件可能会报key not exist警告
        reload();
      });

      const [registerTable, { reload, setProps }] = useTable({
        api: itemList,
        columns: dictItemColumns,
        formConfig: {
          labelAlign: 'right',
          wrapperCol: {},
          schemas: dictItemSearchFormSchema,
          autoSubmitOnEnter: true,
        },
        striped: true,
        useSearchForm: true,
        bordered: true,
        showIndexColumn: false,
        canResize: false,
        immediate: false,
        actionColumn: {
          width: 100,
          title: '操作',
          dataIndex: 'action',
          // slots: { customRender: 'action' },
          fixed: undefined,
        },
      });
      const getTitle = computed(() => '字典配置');

      async function handleDelete(record: Recordable) {
        await deleteItem({ id: record.id }, reload);
      }
      function handleCreate() {
        openModal(true, {
          isUpdate: false,
        });
      }

      function handleEdit(record) {
        openModal(true, {
          record,
          isUpdate: true,
        });
      }
      function handleSuccess() {
        reload();
      }
      return {
        registerModal,
        handleSuccess,
        handleCreate,
        handleDelete,
        handleEdit,
        registerTable,
        registerDrawer,
        getTitle,
        dictId,
      };
    },
  });
</script>
